import {CollectModel} from "../../../models/collect";
const collectModel = new CollectModel()

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    scrollLeft:0,
    tabList:['约拍','作品','场地'],
    shootList:[],
    worksList:[],
    placeList:[],
  },

  tabSelect(e) {
    const TabCur = e.currentTarget.dataset.id
    this.setData({
      TabCur: TabCur,
      scrollLeft: (e.currentTarget.dataset.id-1)*60,
      hasMore:true
    })

    if (this._beforeLoad(TabCur)==0) {
      this._Load(0)
    }
  },

  handlerOnShoot: function (event) {
    let id = event.target.dataset.id
    wx.navigateTo({
      url: '/pages/index/details/about/index?id='+id
    })
  },

  handlerOnWorks: function (event) {
    let id = event.target.dataset.id
    wx.navigateTo({
      url: '/pages/index/details/works/index?id='+id
    })
  },

  onDetails: function (event) {
    let id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/index/details/site/index?placeId='+id
    })
  },

  _loadWorksList(start,moreLoad) {
    collectModel.queryCollectProductionList(start).then(res=>{
      if (typeof (moreLoad) == 'undefined') {
        this.setData({
          worksList:res.data
        })
      }else {
        this.setData({
          worksList:app._LoadMore(this.data.worksList,res.data)
        })
        this.unLoading(res.data);
      }

      wx.hideLoading()
    })
  },

  _loadPlaceList(start,moreLoad) {
    collectModel.getCollectPlaceList(start).then(res=>{
      if (typeof (moreLoad) == 'undefined') {
        this.setData({
          placeList:res.data
        })
      }else {
        this.setData({
          placeList:app._LoadMore(this.data.placeList,res.data)
        })
        this.unLoading(res.data);
      }
      wx.hideLoading()
    })
  },

  _loadShootList(start,moreLoad) {
    collectModel.getCollectShootList(start).then(res=>{
      if (typeof (moreLoad) == 'undefined') {
        this.setData({
          shootList:res.data
        })
      }else {
        this.setData({
          shootList:app._LoadMore(this.data.shootList,res.data)
        })
        this.unLoading(res.data);
      }
      wx.hideLoading()
    })
  },

  unLoading(data) {
    if (data.length == 0) {
      app.showInfo('没有更多了哦~')
    }
  },

  _beforeLoad(TabCur) {
    switch (TabCur) {
      case 0:
        return this.data.shootList.length
      case 1:
        return this.data.worksList.length
      case 2:
        return this.data.placeList.length
      default:
        return false
    }
  },

  _Load(start,moreLoad) {
    wx.showLoading({
      title: '正在加载中...',
    })
    let _this = this
    const TabCur = this.data.TabCur
    switch (TabCur) {
      case 0:
        return _this._loadShootList(start,moreLoad)
      case 1:
        return _this._loadWorksList(start,moreLoad)
      case 2:
        return _this._loadPlaceList(start,moreLoad)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._Load(0)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let _this = this
    wx.showNavigationBarLoading() //在标题栏中显示加载

    //模拟加载
    setTimeout(function()
    {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      _this._Load(0)
    },1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let _this = this
    const TabCur = _this.data.TabCur
    _this._Load(_this._beforeLoad(TabCur),true)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
